package com.zt.rongda.stock.controller;

import com.zt.servlet.*;
import com.zt.rongda.stock.model.StockSupplier;
import com.zt.rongda.stock.service.StockSupplierService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/stockSupplier")
public class StockSupplierController {

    @Autowired
    private StockSupplierService stockSupplierService;

    @RequestMapping(value = "/query")
    @ResponseBody

    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockSupplierService.getTable(reqBean));
    }

    @RequestMapping(value = "/queryById")
    @ResponseBody

    public JSONObject queryById(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockSupplierService.getById(reqBean.getInfo("supplierId")));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockSupplierService.add(getStockSupplier(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockSupplierService.update(getStockSupplier(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockSupplierService.delete(reqBean.getInfo("supplierId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockSupplier getStockSupplier(ReqBean reqBean) {
        StockSupplier stockSupplier = new StockSupplier();
        stockSupplier.setSupplierId(reqBean.getInfo("supplierId"));
        stockSupplier.setSupplierName(reqBean.getInfo("supplierName"));
        stockSupplier.setContactName(reqBean.getInfo("contactName"));
        stockSupplier.setContactPhone(reqBean.getInfo("contactPhone"));
        stockSupplier.setAddress(reqBean.getInfo("address"));
        stockSupplier.setStatus(reqBean.getInfo("status"));
        stockSupplier.setOperator(reqBean.getUserId());
        stockSupplier.setRemark(reqBean.getInfo("remark"));
        return stockSupplier;
    }

}